package com.xmcowell.sportpacemaster.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.xmcowell.sportpacemaster.config.Contents;
import com.xmcowell.sportpacemaster.layout.MainActivity;
import com.xmcowell.sportpacemaster.layout.app.AppActivity;
import com.xmcowell.sportpacemaster.layout.i_run.IRunActivity;
import com.xmcowell.sportpacemaster.layout.me.PersonalActivity;
import com.xmcowell.sportpacemaster.layout.multi.MultiActivity;
import com.xmcowell.sportpacemaster.layout.setting.SettingActivity;
import com.xmcowell.sportpacemaster.layout.setting.TreadmillInfo.TreadmillInfoActivity;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class UsbBroadCastRecerver extends BroadcastReceiver {
    private final static String ACTION ="android.hardware.usb.action.USB_STATE";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.e("UsbBroadCastReceiver","action = "+action);

        if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
            int flag = intent.getIntExtra("plugged",0);//0 断开，1 连上usb
            Log.e("UsbBroadCastReceiver",""+flag);
            if (flag == 1) {
                showUSBConntectStatus(true);
            } else {
                showUSBConntectStatus(false);
            }
        }
    }

    private void showUSBConntectStatus(boolean b) {
        Message msg = new Message();
        msg.what = Contents.USB;
        Bundle bundle = new Bundle();
        bundle.putBoolean("flag",b);
        msg.setData(bundle);
        if (MainActivity.instance != null&&!MainActivity.instance.isFinishing()){
            MainActivity.instance.mHandler.sendMessage(msg);
        }
        if (IRunActivity.instance!=null&&!IRunActivity.instance.isFinishing()){
            IRunActivity.instance.mHandler.sendMessage(msg);
        }
        if (MultiActivity.instance!=null&&!MultiActivity.instance.isFinishing()){
            MultiActivity.instance.mHandler.sendMessage(msg);
        }
        if (AppActivity.instance!=null&&!AppActivity.instance.isFinishing()){
            AppActivity.instance.mHandler.sendMessage(msg);
        }
        if (PersonalActivity.instance!=null&&!PersonalActivity.instance.isFinishing()){
            PersonalActivity.instance.mHandler.sendMessage(msg);
        }
        if (SettingActivity.instance!=null&&!SettingActivity.instance.isFinishing()){
            SettingActivity.instance.handler.sendMessage(msg);
        }
        if (TreadmillInfoActivity.instance!=null&&!TreadmillInfoActivity.instance.isFinishing()){
            TreadmillInfoActivity.instance.mHandler.sendMessage(msg);
        }
    }
}
